<?php
    header("Content-type:text/html;charset=utf-8");
    if ($_POST) {
        include '../../api/config.php';
        include './comm.php';
        //数据库配置文件
        $json = $_POST["data"];
        $data = json_decode($json);
        $id = $data->head;

        if ($id == '1061') {
            if ($id == '1061') {
                $ids = $data->id;
                $sql8 = "DELETE FROM book WHERE id = '{$ids}'";
            }

            if(flag($data)){
                $stmt8 = $conn->query($sql8);
                $stmt8->execute();
                $result8 = $stmt8->setFetchMode(PDO::FETCH_ASSOC);
                if ($result8) {
                    echo get_msg('200','操作成功~',false,false);
                } else {
                    echo get_msg('400','数据异常~',false,false);
                }
            }else{
                echo get_msg('500','权限不足~','../index.html',false);
            };
        }//删除

        if ($id == '1052') {
            $ids = $data->id;
            $sql3 = "SELECT * FROM book WHERE id='{$ids}'";

            if (flag($data)) {
                $stmt4 = $conn->query($sql3);
                $stmt4->execute();
                $result4 = $stmt4->setFetchMode(PDO::FETCH_ASSOC);
                $result4 = $stmt4->fetchAll();
                
                if ($result4) {
                    echo get_msg('200','操作成功~',false,$result4);
                } else {
                    echo get_msg('400','数据异常~',false,false);
                }
            } else {
                echo get_msg('500','权限不足~','../index.html',false);
            }
        }//编辑

        if ($id == '1051') {
            $ids = $data->id;
            $flg = $data->flg;
            $sql3 = "UPDATE book SET flg='{$flg}' WHERE id='{$ids}'";

            if (flag($data)) {
                $arr2 = array();
                $stmt3 = $conn->prepare($sql3);
                $stmt3->execute();
                $result3 = $stmt3->setFetchMode(PDO::FETCH_ASSOC);
                if ($result3) {
                    include './file_item_index.php';
                    echo get_msg('200','操作成功~',false,false);
                } else {
                    echo get_msg('400','数据异常~',false,false);
                }
            } else {
                echo get_msg('500','权限不足~','../index.html',false);
            }
        }//加入回收站

        if ($id == '1062') {
            $key = $data->keys;
            $flg = $data->flg;
            $page = $data->page;
            $page = ($page - 1) * 10;
            $sql2 = "SELECT count(*) FROM book WHERE flg='{$flg}'";
            $sql9 = "SELECT * FROM book WHERE (title LIKE '%{$key}%' OR id LIKE '%{$key}%') AND flg='{$flg}'";

            if (flag($data)) {
                $arr1 = array();
                $arr1['data'] = array();
                $stmt1 = $conn->query($sql9);
                $stmt1->execute();
                $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
                $arr1['data'] = $stmt1->fetchAll();

                $stmt2 = $conn->query($sql2);
                $stmt2->execute();
                $result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
                $all_page = $stmt2->fetchAll()[0]["count(*)"];

                if ($result1) {
                    $arr1['code'] = '200';
                    $arr1['all_page'] = $all_page / 10;
                    $arr1['msg'] = '获取数据成功~';
                    $data = json_encode($arr1);
                    echo $data;
                } else {
                    echo get_msg('400','数据异常~',false,false);
                }
            } else {
                echo get_msg('500','权限不足~','../index.html',false);
            }
        }// 搜索


        if ($id == '1050' || $id == '1060') {
            if ($id == '1050') {
                $page = $data->page;
                $page = ($page - 1) * 10;
                $sql1 = "SELECT * FROM book WHERE flg=1 ORDER BY id DESC LIMIT {$page},10";
                $sql2 = "SELECT count(*) FROM book WHERE flg=1";
            }

            if ($id == '1060') {
                $page = $data->page;
                $page = ($page - 1) * 10;
                $sql1 = "SELECT * FROM book WHERE flg=0 ORDER BY id DESC LIMIT {$page},10";
                $sql2 = "SELECT count(*) FROM book WHERE flg=0";
            }

            if (flag($data)) {
                $arr1 = array();
                $arr1['data'] = array();
                $stmt1 = $conn->query($sql1);
                $stmt1->execute();
                $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
                $arr1['data'] = $stmt1->fetchAll();
                
                $stmt2 = $conn->query($sql2);
                $stmt2->execute();
                $result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
                $all_page = $stmt2->fetchAll()[0]["count(*)"];

                if ($result1) {
                    $arr1['code'] = '200';
                    $arr1['all_page'] = $all_page / 10;
                    $arr1['msg'] = '获取数据成功~';
                    $data = json_encode($arr1);
                    echo $data;
                } else {
                    echo get_msg('400','数据异常~',false,false);
                }
            } else {
                echo get_msg('500','权限不足~','../index.html',false);
            }
        }// 输出内容
        
        $conn = null;
    } else {
        echo "<script>\r\n    window.location.href = '../../';\r\n    </script>";
    }